#include <iostream>
#include <vector>
#include <queue>
using std::vector;
using std::priority_queue;
using std::cout;
int findKthLargest(vector<int>& nums, int k) {
    priority_queue<int, vector<int>, std::greater<int>> minHeap;
    
    for (int num : nums) {
        minHeap.push(num);
        if (static_cast<int>(minHeap.size()) > k) {
            minHeap.pop();  // 保持堆的大小为 k
        }
    }
    return minHeap.top();  // 堆顶就是第K大的元素
}

int main() {
    std::vector<int> nums = {3, 2, 1, 5, 6, 4};
    int k = 2;
    cout << "The nums is ";
    for(auto num:nums){
        cout << " " << num;
    }
    cout << "\n";
    cout << "The " << k << "th largest element is: " << findKthLargest(nums, k) << "\n";
    return 0;
}
